package scau.gsmg.misp.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import scau.gsmg.misp.dao.BaseDAO;
import scau.gsmg.misp.dao.IPermissionDAO;
import scau.gsmg.misp.vo.Permission;
import scau.gsmg.misp.vo.RolePermission;

@Repository
public class PermissionDAO extends BaseDAO implements IPermissionDAO {

	@Override
	public List<Permission> findPermissions(Integer roleId) {
		// TODO Auto-generated method stub
		
		String hql = "";
		Session session = getSession();
//		Permission success=new Permission();
//		success=null;
		List<Permission> permission=new ArrayList<Permission>();

		hql = "from RolePermission where role.id=?";
		//try {
			Query query = session.createQuery(hql);
			query.setParameter(0,roleId);
			List<RolePermission> list = query.list();
		
			if (list.size() != 0) {
				System.out.println(list.size());
				//success=list.get(0);
				
				for(RolePermission ps:list){
					//permission.add(ps.getPermission());
					String hqlPid="from Permission where type='O' and id=?";
					Query queryPid = session.createQuery(hqlPid);
					queryPid.setParameter(0,ps.getPermission().getId());
					List<Permission> listPid = queryPid.list();
					for(int i=0;i<listPid.size();i++){
						permission.add(listPid.get(i));
					}
				}
				session.close();
				return permission;
			}
		/*} catch (Exception e) {
			System.out.println("捕获异常：ww" + e);
			// return 0;
		} finally {
			if (session != null && session.isOpen()) {
				session.close();
			}
		}*/
		session.close();
		return null;
	}

}
